//
//  ConventionalLoginVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/11.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "ConventionalLoginVC.h"
#import "LoginVC.h"
#import "ForgotPasswordVC.h"


@interface ConventionalLoginVC ()<UITextFieldDelegate>
@property (strong, nonatomic) UIButton *SMSLogin;
@property (strong, nonatomic) UIButton *wjPress;
@property (strong, nonatomic) UIButton *clearBtn;
@property (strong, nonatomic) UIButton *hiddenON;
@property (strong, nonatomic) UIButton *login;
@property (strong, nonatomic) UITextField *userName;
@property (strong, nonatomic) UITextField *password;

@end

@implementation ConventionalLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setNavTitle:@"登录" titleColor:[UIColor whiteColor] font:font17];
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationController.navigationBar.barTintColor =  UIColorFromRGB(0xe71a41);
   // self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    
    //self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor =  UIColorFromRGB(0xe71a41);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameDidChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [self initView];
}


- (void)initView{
    UIImageView *titltImg = [[UIImageView alloc] init];
    titltImg.image = [UIImage imageNamed:@"login_icon_default_nor.png"];
    [self.view addSubview:titltImg];
    titltImg.sd_layout
    .topSpaceToView(self.view, GETHEIGHT(172))
    .centerXEqualToView(self.view)
    .widthIs(GETWITH(140))
    .heightEqualToWidth();
    
    UIImageView *userImg = [[UIImageView alloc] init];
    userImg.image = [UIImage imageNamed:@"login_icon1_input_nor.png"];
    [self.view addSubview:userImg];
    userImg.sd_layout
    .topSpaceToView(titltImg, GETHEIGHT(180))
    .leftSpaceToView(self.view, GETWITH(40))
    .widthIs(GETWITH(42))
    .heightIs(GETHEIGHT(46));
    
    _userName = [[UITextField alloc] init];
    _userName.font = [UIFont systemFontOfSize:16*H];
    _userName.textColor = UIColorFromRGB(0x333333);
    _userName.delegate = self;
    _userName.returnKeyType = UIReturnKeyDone;
    _userName.placeholder = @"请输入手机号码";
    [self.view addSubview:_userName];
    _userName.sd_layout
    .leftSpaceToView(userImg, GETWITH(46))
    .centerYEqualToView(userImg)
    .rightSpaceToView(self.view, GETWITH(130))
    .heightIs(GETHEIGHT(60));
    
    _clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_clearBtn setImage:[UIImage imageNamed:@"login_icon3_input_nor"] forState:UIControlStateNormal];
    [_clearBtn addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_clearBtn];
    _clearBtn.sd_layout
    .rightSpaceToView(self.view, GETWITH(52))
    .centerYEqualToView(userImg)
    .widthIs(GETWITH(40))
    .heightEqualToWidth();
    
    UIView *fgView = [[UIView alloc] init];
    fgView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:fgView];
    fgView.sd_layout
    .leftEqualToView(userImg)
    .rightSpaceToView(self.view, GETWITH(40))
    .topSpaceToView(_userName, GETHEIGHT(27))
    .heightIs(GETHEIGHT(1));
    
    
    UIImageView *suoImg = [[UIImageView alloc] init];
    suoImg.image = [UIImage imageNamed:@"login_icon2_input_nor.png"];
    [self.view addSubview:suoImg];
    suoImg.sd_layout
    .leftEqualToView(userImg)
    .topSpaceToView(fgView, GETHEIGHT(50))
    .widthIs(GETWITH(42))
    .heightIs(GETHEIGHT(46));

    _password = [[UITextField alloc] init];
    _password.font = [UIFont systemFontOfSize:16*H];
    _password.textColor = UIColorFromRGB(0x333333);
    _password.placeholder = @"请输入密码";
    _password.delegate = self;
    _password.secureTextEntry  = YES;
    _password.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:_password];
    _password.sd_layout
    .leftSpaceToView(suoImg, GETWITH(46))
    .centerYEqualToView(suoImg)
    .rightSpaceToView(self.view, GETWITH(130))
    .heightIs(GETHEIGHT(60));

    _hiddenON = [UIButton buttonWithType:UIButtonTypeCustom];
    [_hiddenON setImage:[UIImage imageNamed:@"login_icon5_input_nor"] forState:UIControlStateNormal];
    [_hiddenON addTarget:self action:@selector(isNoOrYes:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_hiddenON];
    _hiddenON.sd_layout
    .rightEqualToView(fgView)
    .centerYEqualToView(suoImg)
    .widthIs(GETWITH(44))
    .heightIs(GETHEIGHT(30));
    
    UIView *fView = [[UIView alloc] init];
    fView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:fView];
    fView.sd_layout
    .leftEqualToView(suoImg)
    .rightEqualToView(_hiddenON)
    .topSpaceToView(_password, GETHEIGHT(28))
    .heightIs(GETHEIGHT(1));

    CGFloat Widh = 0;
    CGFloat height = 0;
    _SMSLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    [_SMSLogin setTitleColor:UIColorFromRGB(0xe64463) forState:UIControlStateNormal];
    [_SMSLogin setTitle:@"短信验证码登录" forState:UIControlStateNormal];
    Widh = [ToolClass heighW:font12 titleStr:@"短信验证码登录"].width;
    height = [ToolClass heighW:font12 titleStr:@"短信验证码登录"].height;
    [_SMSLogin addTarget:self action:@selector(msmLoginStyle:) forControlEvents:UIControlEventTouchUpInside];
    _SMSLogin.titleLabel.font = font12;
    [self.view addSubview:_SMSLogin];
    _SMSLogin.sd_layout
    .leftEqualToView(fView)
    .topSpaceToView(fView, GETHEIGHT(20))
    .widthIs(Widh)
    .heightIs(height);
    
    _wjPress = [UIButton buttonWithType:UIButtonTypeCustom];
    _wjPress.hidden = YES;
    [_wjPress setTitleColor:UIColorFromRGB(0xe64463) forState:UIControlStateNormal];
    [_wjPress setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [_wjPress addTarget:self action:@selector(forgot) forControlEvents:UIControlEventTouchUpInside];
    _wjPress.titleLabel.font = font12;
    Widh = [ToolClass heighW:font12 titleStr:@"忘记密码吗"].width;
    height = [ToolClass heighW:font12 titleStr:@"忘记密码?"].height;
    [self.view addSubview:_wjPress];
    _wjPress.sd_layout
    .rightEqualToView(fView)
    .topSpaceToView(fView, GETHEIGHT(20))
    .widthIs(Widh)
    .heightIs(height);
    
    _login = [UIButton buttonWithType:UIButtonTypeCustom];
    [_login setBackgroundImage:[UIImage imageNamed:@"login_btn_defauit_nor"] forState:UIControlStateNormal];
    [_login addTarget:self action:@selector(ONLogin:) forControlEvents:UIControlEventTouchUpInside];
    //_login.enabled = NO;
    [self.view addSubview:_login];
    _login.sd_layout
    .topSpaceToView(fView, GETHEIGHT(160))
    .leftEqualToView(fgView)
    .heightIs(GETHEIGHT(100))
    .rightEqualToView(fgView);
    
}

- (IBAction)isNoOrYes:(UIButton *)sender {
    
    _hiddenON.selected = !sender.selected;
    if(_hiddenON.selected){
        [_hiddenON setImage:[UIImage imageNamed:@"login_icon4_input_nor"] forState:UIControlStateNormal];
        _password.secureTextEntry = NO;
    }else{
        [_hiddenON setImage:[UIImage imageNamed:@"login_icon5_input_nor"] forState:UIControlStateNormal];
        _password.secureTextEntry = YES;
        
    }
}


//监听键盘高度变化
-(void)keyboardFrameDidChange:(NSNotification*)notice
{
    NSDictionary * userInfo = notice.userInfo;
    NSValue * endFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect endFrame = endFrameValue.CGRectValue;
    if(endFrame.origin.y < (_password.bottom + 70) || endFrame.origin.y < (_userName.bottom+64)){
        [UIView animateWithDuration:0.25 animations:^{
            self.view.y = 0;
        }];
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            self.view.y = 64;
        }];

    }
   
    
}


- (void)ONLogin:(id)sender {
    [_password resignFirstResponder];
    [_userName resignFirstResponder];
    
    if(_userName.text.length<1){
         [AlertUtils showToastWithMessage:@"请先填写手机号码" inView:self.view hideAfterDelay:1.0 beIndeter:NO];
        return;
    }
    if(_password.text.length<1){
          [AlertUtils showToastWithMessage:@"请先填写密码" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    
    if(![ToolClass valiMobile:_userName.text]){
         [AlertUtils showToastWithMessage:@"请填入正确的手机号" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }

#ifdef DEBUG
    NSString *token = @"1f615e963e4c6db6df214c17af2d72b02c306b25d7bd8c8b2e782c8cbb836aae";
#else
    NSString *token = [userDefaults objectForKey:@"HLBdeviceToken"]?[userDefaults objectForKey:@"HLBdeviceToken"]:@"";
#endif

    [[PPHttpRequsetData shareInstance] requestStyle:Request_GET URL:@"/ShopApi/Login/login" parameter:@{@"phone":_userName.text,@"loginType":@"2",@"password":_password.text,@"deviceToken":token} successBlock:^(PPHttpRequest *request, id responseObj) {
        [userDefaults setObject:responseObj[@"guid"] forKey:@"guid"];
        [userDefaults setObject:responseObj[@"admin_id"] forKey:@"admin_id"];
        [userDefaults setObject:_userName.text forKey:@"useriPhone"];
        [[AppDelegate delegate] showMainCtr];

    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    } ];

   
    
   
}

- (void)forgot{
    ForgotPasswordVC *vc = [[ForgotPasswordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)msmLoginStyle:(UIButton *)button{
    LoginVC *vc = [[LoginVC alloc] init];

    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)clear:(id)sender {
    _password.text = @"";
    _userName.text = @"";
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if(_password.text.length >0 || _userName.text.length > 0 ){
        [_login setBackgroundImage:[UIImage imageNamed:@"login_btn_defauit_hig"] forState:UIControlStateNormal];
      //  _login.enabled = YES;
    }else{
        [_login setBackgroundImage:[UIImage imageNamed:@"login_btn_defauit_nor"] forState:UIControlStateNormal];
      //  _login.enabled = NO;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
